#ifndef _ASYNFRAMEIMPL_H_
#define _ASYNFRAMEIMPL_H_

#include "stdafx.h"

class CAsynFrameImpl : public IAsynFrame, public IPlugin, public IPluginRun, public CUnknownImp, public CThreadPool
{
public:
    CAsynFrameImpl(void);
    virtual ~CAsynFrameImpl(void);

    BEGIN_STDCOM_MAP
    STDCOM_INTERFACE_ENTRY_UNKNOWN_(IAsynFrame)
    STDCOM_INTERFACE_ENTRY(IPlugin)
    STDCOM_INTERFACE_ENTRY(IPluginRun)
    STDCOM_INTERFACE_ENTRY(IAsynFrame)
    END_STDCOM_MAP

    // IPlugin
    std_method(Init)(IBase* pRot, IBase* pBase);
    std_method(Uninit)();

    // IPluginRun
    std_method(Start)(_pid_t pid, unsigned int type);
    std_method(Stop)(unsigned int exit);

    // IAsynFrame
    std_method(CreateIo)(IO_Protocol iop, IBase** pBase);
    std_method(PostIo)(IBase* pOperation);

public:
    std_method(PostComplete)(int type);

protected:
    std_method(ThreadPool_Run)(void* user, void* ctx);
    std_method(ThreadPool_Start)();
    std_method(ThreadPool_Stop)();
    std_method(ThreadPool_Error)();

private:
    std_method(CreateIOperation)(IBase** pBase);
    std_method(CreateITcpSocket)(IBase** pBase);
    std_method(CreateIUdpSocket)(IBase** pBase);
    std_method(CreateITcpListenSocket)(IBase** pBase);
    std_method(CreateITcpSocketSSL)(IBase** pBase);
    std_method(CreateITcpListenSSLSocket)(IBase** pBase);
    std_method(CreateITimer)(IBase** pBase);
    std_method(CreateIPipe)(IBase** pBase);
    std_method(CreateISPipe)(IBase** pBase);

private:
    _lComPtr<IRot> m_pRot;

private:
    std_method(InitEventLoop)();
    std_method(CreateEventLoop)(ioevloop_t* ev, evloop_notify cb, void* user);
    std_method(ExitEventLoop)(ioevloop_t* ev);
    std_method(RunEventLoop)(ioevloop_t* ev);

private:
    ioevloop_t* get_service();

private:
    CSemHandle m_sem;
    CSemHandle m_exitsem;
    unsigned int m_tpcount;
    ioevloop_t* m_evloop;
    CAutoLock m_lock;
    unsigned int m_eventindex;
};

#endif
